package com.liferay.controller;

import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.model.Subject;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.service.RegisterSubjectService;
import com.liferay.service.impl.RegisterSubjectServiceImpl;
import com.liferay.util.bridges.mvc.MVCPortlet;

@Controller(value="RegisterSubjectController")
@RequestMapping("VIEW")
public class RegisterSubjectController extends MVCPortlet {
 
	private RegisterSubjectService registerSubjectService;
	private List<Subject> listSubjectOpen;
	private List<Subject> listSubjectOpenResgister;	
	
	
	@RenderMapping
	public String getDefaultPage(RenderRequest request, RenderResponse response, Model model){
		String userId = request.getRemoteUser();
		registerSubjectService = new RegisterSubjectServiceImpl();
		listSubjectOpen = registerSubjectService.getListSubjectOpen(Long.parseLong(userId));
		model.addAttribute("listSubjectOpen", listSubjectOpen);
		listSubjectOpenResgister = registerSubjectService.getListSubjectOpenRegister(Long.parseLong(userId));
		model.addAttribute("listSubjectOpenResgister", listSubjectOpenResgister);
		return "view";
	}
	
	@ActionMapping(params="action=register")
	public void registerSubject(ActionRequest request, ActionResponse response){
		long idSubjectOpen = ParamUtil.getLong(request, "idSubjectOpen");
		registerSubjectService = new RegisterSubjectServiceImpl();
		Subject subject = new Subject();
		subject.setIdSubjectOpen(idSubjectOpen);
		String userId = request.getRemoteUser();
		subject.setFkStudent(Long.parseLong(userId));
		registerSubjectService.registerSubject(subject);
		response.setRenderParameter("action", "list");
	}
	
	@ActionMapping(params="action=delete")
	public void deleteSubject(ActionRequest request, ActionResponse response){
		long idSubjectOpen = ParamUtil.getLong(request, "idSubjectOpen");
		registerSubjectService = new RegisterSubjectServiceImpl();
		Subject subject = new Subject();
		subject.setIdSubjectOpen(idSubjectOpen);
		String userId = request.getRemoteUser();
		subject.setFkStudent(Long.parseLong(userId));
		registerSubjectService.deleteSubject(subject);
		response.setRenderParameter("action", "list");
	}
}
